Ccay.Module.define("Ccay.hypertext.ListHyperText",function(page,$S){
	/**
	 * 导入枚举类型
	 */
	Enum.import("HyperCategoryEnum");
	
	/**
	 * Tab & Grid初始化
	 */
	page.ready=function(){	
		$S("#listHyperTextTab").ccayTab();		
	};
	
	page.initGrid = function(category){
		var gridOptions={
				customSetting: true,
				height : 300,
				columns:[
				 {columnType:'select',multi:true,freezeable : true,cssName:"center"},
			     {width:60,freezeable : true,columnType: 'operate',content:"${#renderEdit(data)}"},
			     {field: 'category' , header:"ccay.hypertext.label.category",type:'enum',format:{service: 'ENUM:HyperCategoryEnum'}, width:120},
			     {field: 'name' , header: "ccay.hypertext.label.name",width:180,content:"${#renderView(data)}"}
				],
				ajax : {
					url : "services/manage/hypertext/list/page/{0}/{1}"
				},
				operation:{
					search:{
						btn:'#btnSearch',
						panel:'#hyperTextCondForm'
				}
			}
		  }
		  if(Enum.HyperCategoryEnum.StaticResource == category){
			  gridOptions.columns.push({field: 'mappingUrl' , header: "ccay.hypertext.label.mappingUrl",width:180, renderer:'ellipsis'});
		  }
		  if(Enum.HyperCategoryEnum.MailTemplate == category){
			  gridOptions.columns.push({field: 'title' , header: "ccay.hypertext.label.title" , width:200, renderer:'ellipsis'});
		  }
		  gridOptions.columns.push({field: 'description' , header: "ccay.hypertext.label.description" , width:200, renderer:'ellipsis'});
		  gridOptions.columns.push({field: 'creationUser' , header: "ccay.common.label.creater",width:120});
		  gridOptions.columns.push({field: 'lastUpdateUser' , header: "ccay.common.label.lastUpdatedBy",width:120});
		  gridOptions.columns.push({field: 'lastUpdateDate' ,width:140, header: "ccay.common.label.lastUpdatedDate" ,cssName:"center",content:"${#thisToDateBefore(data.lastUpdateDate)}"});
		  $S("#hyperTextGrid").igrid(gridOptions);
	}
	
	
	/**
	 * onchange事件，下拉框为MailTemplate或Resources时，显示textarea
	 */
	page.onTypeChange=function(input,value,text, oldValue,item,selector,data,noConnect){
		//debugger;
		if(!$S("#hyperTextCondForm").valid()){
			return ;
		}
		for(attr in Enum.HyperCategoryEnum){
			if(Enum.HyperCategoryEnum[attr] == input){
				$S("."+attr).show();
			}else{
				$S("."+attr).hide();
			}
		}
		
		page.initGrid(input);
	}
	
	/**
	 * @description grid删除按钮 触发方法
	 */
	page.del=function(){
		var grid = "#hyperTextGrid";
		var records = $(grid).manager().getSelectedRecords();
		var url = "services/ccay/hypertext/d/list";
		if (records.length > 0) {
			Ccay.UI.confirm("ccay.common.message.confdel", function(result) {
						if (result) {
							Ccay.doPut(url, records, function() {
										// 调用刷新grid的方法
										$(grid).manager().reload(true, true);
									});
						}
					});
		} else {
			Ccay.UI.showMessage("ccay.common.message.selectedRecords", "warn");
		}
	}
	
	/**
	 * @description  打开edit页面dialog 
	 * @param id 传入的参数
	 */
	page.openEdit=function(category,code){
		Ccay.Page.register("Ccay.hypertext.EditHyperText","manage/hypertext/editHyperText.html");
		 Ccay.Common.openPage(code ? "ccay.common.button.edit":"ccay.common.button.add","Ccay.hypertext.EditHyperText",
					{ tab:'#listHyperTextTab',
					  tabid:"Edit",
					  width:950,
				      height:530,
				      buttons:[{
				      	  type:"imgBtn",
				      	  icon:"confirm",
				      	  cls:"ccay-icon",
					      text:"ccay.common.button.save",
					      onClick:function(){Ccay.hypertext.EditHyperText.save();
				      }},{
				      	  type:"imgBtn",
				      	  icon:"cancel",
				      	  cls:"ccay-icon",
					      text:"ccay.common.button.cancel",
					      onClick:function(){Ccay.hypertext.EditHyperText.close()}
				      }]
					},				     
					function(){
						Ccay.hypertext.EditHyperText.init(category,code);
					});	
	};
	
	/**
	 * @description  打开view页面dialog 
	 * @param id 传入的参数
	 */
	page.openView=function(category,id){
		Ccay.Page.register("Ccay.hypertext.ViewHyperText","manage/hypertext/viewHyperText.html");
		Ccay.Common.openPage("ccay.common.label.view","Ccay.hypertext.ViewHyperText",
					{ tab:'#listHyperTextTab',
					  tabid:"View",
					  width:950,
				      height:500,
				      buttons:[{
				      	  type:"imgBtn",
				      	  icon:"cancel",
				      	  cls:"ccay-icon",
					      text:"ccay.common.button.cancel",
					      onClick:function(){Ccay.hypertext.ViewHyperText.close()}
					  }]
				    },				     
					function(){
						Ccay.hypertext.ViewHyperText.init(category,id);
					});	
	};
	
	/**
	 * 关闭tab
	 */
	page.closeTab = function(isReload)
	{
		$S("#listHyperTextTab").ccayTabRemoveItem();
		if(isReload)
			$S("#hyperTextGrid").ccayGridSearch();
	}
	
    /**
	 *打开edit的tab
	 */
	renderEdit = function(record){
		   return Ccay.UI.button("ccay.common.button.edit","Ccay.hypertext.ListHyperText.openEdit('"+record.category+"','"+record.id+"')",
				   "hypertextManagement:create","img","edit");
	}
	
    /**
	 *打开view的tab
	 */
	renderView = function (record){
		return Ccay.UI.button(record.name,"Ccay.hypertext.ListHyperText.openView('{0}','{1}')".formatValue(record.category,record.id)
				,"","link","",false);
	}

    /**
     * hyperText导入
     */
	page.hyperTextImport=function(){
		var param={uploadType:"ExcelImport",
				   callback:hyperTextImportCallback,
				   closeDialog:true,
				   query:"excelType=hyperTextImport"};
		Ccay.Component.openUploadDialog($i18n("ccay.common.button.import"),param);
	} 
	
    /**
	*hyperText 导出
	*/
	page.exportHyperTextByIExcel=function(){
		Ccay.doGet("services/ccay/hypertext/export/single",$S("#hyperTextForm").jsonData(),'');
	} 	
	
	/**
	 * hyperText导入的callback
	 */
	function hyperTextImportCallback(file,serverData){
		Ccay.Core.log("File upload callback.");
		Ccay.Core.log(file);
		Ccay.UI.topBox("ccay.hypertext.message.fileUploadedSuccess");
	}
	
	 /**
     * hyperText原生excel导出
     */
	page.exportHyperText = function(){
		var data = $S("#hyperTextCondForm").jsonData();
		Ccay.doGet("services/ccay/hypertext/exportHyperText",data,function(){
			Ccay.Component.Excel.exportCallback();
		});
	}
   
	/**
	*hyperText 导入
	*/
	page.hyperTextImport=function(){
		var param={uploadType:"ExcelImport",
					completeCallback: Ccay.Component.Excel.uploadCallback,
					closeDialog:true,
					query:"excelType=ccay.hypertext"};
		Ccay.Component.openUploadDialog($i18n("ccay.common.button.import"),param);
	}
	
	showUserInfo = function(value){
		return "<a href='javascript:Ccay.Component.popUserInfo(\"{0}\")' class='link-button'>{1}</a>".formatValue(value, value);
	};
	
	thisToDateBefore = function(data) {
		return Ccay.Format.toDateBefore(data && data.toDate().getUTCTime());
	};
});




